import axios from 'axios';
import {getStoreId, getToken, removeToken} from "@/store/local";
import router from "@/router";

const request = axios.create({
    baseURL: 'api/admin/',
    timeout: 5000,
})

request.interceptors.request.use(config => {
    config.headers.setAuthorization(getToken())
    config.headers['Store-ID'] = getStoreId()
    return config
})

request.interceptors.response.use(res => {
    const resp = res.data
    if (resp.code === 444) {
        window.$message.error("登陆过期，请重新登陆")
        removeToken()
        router.push("/login");
    } else if (resp.code !== 200) {
        let message = "网络异常，请稍后再试！"
        if (resp && resp.message) {
            message = resp.message
        }
        window.$message.error(message)
        return Promise.reject('network failed!!')
    }
    return Promise.resolve(resp)
}, error => {
    window.$message.error("网络异常!")
    return Promise.reject(error)
})


export const reqAdminInfo = () => request({url: '/user/info', method: 'get'})

export const reqLogin = (phoneNum, password) => request({
    url: '/user/login',
    method: 'post',
    data: {phoneNum, password}
})

export const reqGoodsList = (params) => request({url: '/goods/list', method: 'get', params})

export const reqGoodsDetail = (goodsId) => request({url: '/goods/detail', method: 'get', params: {goodsId}})

export const reqGoodsCateList = (parentId) => request({url: '/goods/cate', method: 'get', params: {parentId}})

export const reqGoodsSave = (data) => request({url: '/goods/save', method: 'post', data})

export const reqGoodsEdit = (data) => request({url: '/goods/edit', method: 'post', data})

export const reqGoodsStatusUpdate = (data) => request({url: '/goods/updateStatus', method: 'post', data})

export const reqMediaGroupList = () => request({url: '/media/group/list', method: 'get'})

export const reqMediaList = (params) => request({url: '/media/list', method: 'get', params})

export const reqDeleteMediaGroup = (id) => request({url: '/media/group/delete', method: 'post', data: {id}})

export const reqSaveMediaGroup = (data) => request({url: '/media/group/saveOrEdit', method: 'post', data})

export const reqDeleteMedia = (ids) => request({url: '/media/delete', method: 'post', data: ids})

export const reqSaveMedia = (data) => request({url: '/media/save', method: 'post', data})

export const reqEditMedia = (data) => request({url: '/media/edit', method: 'post', data})

export const reqDictParentList = (params) => request({url: '/dict/parent/list', method: 'get', params})

export const reqDictChildrenList = (pId) => request({url: '/dict/children/list', method: 'get', params: {pId}})

export const reqDictDel = (id) => request({url: '/dict/delete', method: 'post', params: {id}})

export const reqDictSave = (data) => request({url: '/dict/save', method: 'post', data})

export const reqFilePolicy = (fileName) => request({url: '/file/policy', method: 'get', {"suffix": fileName}})

export const fileUpload = (data) => {
    return axios.post("https://fb513-test-bucket.oss-cn-hangzhou.aliyuncs.com", data)
}